/**
 * zmn渠道管理-押金管理
 * 文件命名格式：zmn.模块.表/功能.js
 * @author xiangjunming
 * @since 2019/08/22 18:06
 */
layui.define(['form', 'layedit', 'laydate', 'table', 'laytpl'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    let table = layui.table;
    var laytpl = layui.laytpl;

    let type = null;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: 10,
        height: 'full-140'
    });

    form.render(null, "zmn-form");

    table.on("toolbar(zmn-table)", function (obj) {
        let data;
        switch (obj.event) {
            case 'in':
                data = {type: 1, title: '收款'};
                break;
            case 'out':
                data = {type: 2, title: '退款'};
                break;
            case 'deduct':
                data = {type: 3, title: '扣款'};
                break
            default:
                return;
        }
        type = data.type;

        laytpl(inOutHtml.innerHTML).render(data, function (html) {
            childIndex = layer.open({
                type: 1,
                title: data.title
                , content: html
                , area: ['500px', '300px']
            });
        });

    });
    // 自定义验证规则
    form.verify({
        amount: function(value){
            if (value <= 0) {
                return "金额不能小于等于0";
            }
            let reg = new RegExp("^[0-9]{0,6}$");
            if (!reg.test(value)) {
                return "请输入不超过6位正整数";
            }
        }
    });

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        var data = obj.field;
        var url = "/channel/bail/modify/addEdit.action";
        data.channelId = $('#channelId').val();
        $.ajax({
            async: false,
            type: "POST", url: url,
            dataType: "json", data: data,
            success:function(resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if(resp.status == 200) {
                    // zmnPage.direct("/channel/deposit.action?channelId=" + resp.id + "&addFlag=" + json.addFlag, 0);
                    zmnPage.reload(resp.message, 1000);
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    });
    
    //对外暴露的接口
    exports('channel_bail', {});
});